package com.lagou.controller;

import com.lagou.domain.ResponseResult;
import com.lagou.domain.User;
import com.lagou.domain.UserVo;
import com.lagou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/findAllUserByPage")
    public ResponseResult findAllUserByPage(@RequestBody UserVo userVo) {
        return new ResponseResult(true, 200, "success", userService.findAllUserByPage(userVo));
    }

    @RequestMapping("/login")
    public ResponseResult login(User user, HttpServletRequest request) throws Exception {
        User user2 = userService.login(user);
        if (user2 != null) {
            HttpSession session = request.getSession();
            String access_token = UUID.randomUUID().toString();
            session.setAttribute("access_token", access_token);
            session.setAttribute("user_id", user2.getId());
            Map<String, Object> map = new HashMap<>();
            map.put("access_token", access_token);
            map.put("user_id", user2.getId());
            map.put("user", user2);
            return new ResponseResult(true, 1, "success", map);
        }
        return new ResponseResult(true, 200, "用户名或密码错误", null);
    }

    @RequestMapping("/findUserRoleById")
    public ResponseResult findUserRelationRoleById(Integer userId) {
        return new ResponseResult(true, 200, "success", userService.findUserRelationRoleById(userId));
    }

    @RequestMapping("/userContextRole")
    public ResponseResult userContextRole(@RequestBody UserVo userVo) {
        userService.userContextRole(userVo);
        return new ResponseResult(true, 200, "success", null);
    }

    @RequestMapping("/getUserPermissions")
    public ResponseResult getUserPermissions(HttpServletRequest request) {
        String header_token = request.getHeader("Authorization");
        String session_token = (String) request.getSession().getAttribute("access_token");
        if (header_token.equals(session_token)) {
            Integer userId = (Integer) request.getSession().getAttribute("user_id");
            return userService.getUserPermissions(userId);
        } else {
            return new ResponseResult(false, 400, "error", null);
        }
    }
}
